<?php

/**
 * 检查定时发布的清单 是否已到发布时间
 */

use hhz\constant\redis\redisKeys;
use Hhz\Redis\otherRedis;
use Biz\cache\album\AlbumTimePublishCache;
use Biz\model\Album\AlbumListModel;
use Biz\service\Album\AlbumListService;
use Doraemon\tools\Log;
use Biz\model\Album\AlbumWikiListModel;
use Biz\service\Album\AlbumWikiListService;

ini_set('display_errors', 1);
ini_set('memory_limit', -1); //不限制内存
ini_set("error_reporting", E_ALL);//显示所有错误

require_once '/data/wwwroot/g_vendor/autoload.php';

main();
/*
for ($i = 0; $i < 60; $i++) {
    //每分钟执行1次，执行60分钟，60次
    var_dump('执行第' . ($i + 1) . '次');
    main();
    sleep(60);
}
*/

function main()
{
    $time = time();
    $key = redisKeys::KEY_B_OTHER_ALBUM_TIME_PUBLISH;
    $redis = new otherRedis($key);
    $publishAlbumIdArr = $redis->zrangebyscore($key, 0, $time);
//$publishAlbumIdArr = AlbumTimePublishCache::getAlbumForPublish($time);
    if ($publishAlbumIdArr) {
        Log::info('$publishAlbumIdArr', ['$publishAlbumIdArr' => $publishAlbumIdArr]);
        AlbumListModel::update(['status' => AlbumListModel::STATUS_SUCCESS], ['album_id' => $publishAlbumIdArr]);
        $redis->zremrangebyscore($key, 0, $time);
//    AlbumTimePublishCache::removeAlbumForPublish($time);
        AlbumListService::setTopcCache(); //更新缓存
        foreach ($publishAlbumIdArr as $albumId) {
            $wikiList = AlbumWikiListModel::selectByWhere([
                'album_id' => $albumId,
                'obj_id[!]' => '',
                'status' => AlbumWikiListModel::STATUS_NORMAL
            ], ['album_id', 'obj_id', 'obj_type', 'status']);
            foreach ($wikiList as $row) {
                if ($row['obj_id']) {
                    AlbumWikiListService::setAlbumWikiObjForOfficial($albumId, $row['obj_id']);
                }
            }
        }
        echo '处理定时发布清单成功,共处理' . count($publishAlbumIdArr) . '条数据' . PHP_EOL;
    }
}